package com.ws.shop.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.stereotype.Repository;

import com.ws.shop.bean.Page;
import com.ws.shop.dao.LoginlogDao;
import com.ws.shop.entity.Loginlog;
import com.ws.shop.util.JdbcUtil;

@Repository(value="loginlogDao")
public class LoginlogDaoImpl extends BaseDaoImpl implements LoginlogDao {
	private static Logger log = Logger.getLogger(LoginlogDaoImpl.class);
	
	final String findLoginlogListPage_count = "select count(1) from jvf_loginlog";
	final String findLoginlogListPage_list = "select id,uid,name,address,ip,addtime from jvf_loginlog";
	final String findLoginlogListPage_order = " order by id desc";
	public Page findLoginlogListPage(int pageno,int pagesize, String name, String start,
			String end) {
		StringBuilder query = new StringBuilder(" where 1=1");
		if(name != null)
			query.append(" and name='").append(name).append("'");
		if(end != null)
			query.append(" and addtime<='").append(end).append("'");
		if(start != null)
			query.append(" and addtime>='").append(start).append("'");
		return findPage(pageno,pagesize,new StringBuilder(findLoginlogListPage_count).append(query).toString()
				,new StringBuilder(findLoginlogListPage_list).append(query).append(findLoginlogListPage_order));
	}
	
	public void getLoginInfoByUserId(final Page page ,final int uid){
		this.execute(
				new ConnectionCallback(){
					@SuppressWarnings("unchecked")
					public Object doInConnection(Connection conn)
					throws SQLException, DataAccessException {
						Statement stmt = null;
						ResultSet rs = null;
						try{
							String s = "SELECT COUNT(1) FROM jvf_loginlog WHERE uid = " + uid;
							stmt = conn.createStatement();
							rs = stmt.executeQuery(s);
							long totalnum = 0;
							if (rs.next()) {
								totalnum = rs.getLong(1);
								page.setTotalnum(totalnum);
							}
							JdbcUtil.close(rs,stmt,null);
							if(totalnum>0){
								s = "SELECT id,uid,NAME,address,ip,ADDTIME FROM  jvf_loginlog WHERE uid = "+ uid +" limit "+page.getStartrownum()+","+page.getPagesize();
								stmt = conn.createStatement();
								rs = stmt.executeQuery(s);
								while (rs.next()) {
								    Loginlog loginlog = new Loginlog();
								    loginlog.setId(rs.getLong(1));
								    loginlog.setUid(rs.getInt(2));
								    loginlog.setName(rs.getString(3));
								    loginlog.setAddress(rs.getString(4));
								    loginlog.setIp(rs.getString(5));
								    loginlog.setAddtime(rs.getLong(6));
									page.addData(loginlog);
								}
							}
						}catch (Exception e) {
							e.printStackTrace();
							log.error(e.getMessage(), e);
						} finally {
							JdbcUtil.close(rs, stmt, null);
						}
						return null;
					}
				}
		);
	}
}
